package com.eliza.notice.activity

import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.eliza.library.tools.InfoTools
import com.eliza.notice.R


class AlertDialogActivity : AppCompatActivity() {
    var context = this
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_alertdialog)
        val onClick = onClickListener()
        findViewById<Button>(R.id.btn_dialog_one).setOnClickListener(onClick)
        findViewById<Button>(R.id.btn_dialog_two).setOnClickListener(onClick)
        findViewById<Button>(R.id.btn_dialog_three).setOnClickListener(onClick)
        findViewById<Button>(R.id.btn_dialog_four).setOnClickListener(onClick)

    }

    inner class onClickListener : View.OnClickListener {
        override fun onClick(p0: View) {
            val builder = AlertDialog.Builder(context)
            when (p0.id) {
                R.id.btn_dialog_one ->
                    builder.setIcon(R.mipmap.ic_icon_fish)
                        .setTitle("系统提示：")
                        .setMessage("这是一个最普通的AlertDialog,\n带有三个按钮，分别是取消，中立和确定")
                        //设置否定按钮
                        .setNegativeButton("取消") { dialog, which ->
                            InfoTools.ToastTools(context, "点击了 取消！")
                        }.setPositiveButton("确定") { dialog, which ->
                            InfoTools.ToastTools(context, "点击了 确定！")
                        }.setNeutralButton("中立") { dialog, which ->
                            InfoTools.ToastTools(context, "点击了 中立！")
                        }.create().show() //创建AlertDialog对象 并显示

                R.id.btn_dialog_two -> also {
                    var lesson = arrayOf<String>("语文", "数学", "英语", "化学", "生物", "物理", "体育")
                    builder.setIcon(R.mipmap.ic_icon_fish)
                        .setTitle("选择你喜欢的课程")
                        .setItems(lesson) { dialog, which ->
                            InfoTools.ToastTools(context, "选择了 ${lesson[which]}！")
                        }.create().show()
                }
                //单选列表对话框

                R.id.btn_dialog_three -> also {
                    var fruits = arrayOf<String>("苹果", "雪梨", "香蕉", "葡萄", "西瓜")
                    builder.setIcon(R.mipmap.ic_icon_fish)
                        .setTitle("选择你喜欢的水果，只能选一个哦~")
                        .setSingleChoiceItems(fruits, 0) { dialog, which ->
                            InfoTools.ToastTools(context, "选择了 ${fruits[which]}！")
                        }.create().show()
                }
                //多选列表对话框
                R.id.btn_dialog_four -> also {
                    var menu = arrayOf<String>("水煮豆腐", "萝卜牛腩", "酱油鸡", "胡椒猪肚鸡")
                    //定义一个用来记录个列表项状态的boolean数组
                    var checkItems = booleanArrayOf(false, false, false, false)
                    builder.setIcon(R.mipmap.ic_icon_fish)
                        .setTitle("选择你喜欢的课程")
                        .setMultiChoiceItems(menu, checkItems) { dialog, which, isChecked ->
                            checkItems[which] = isChecked;
                        }.setPositiveButton("确定") { dialog, which ->
                            /* 字符拼接*/
                            var result = ""
                            for (i in checkItems.indices) {
                                if (checkItems[i]) result += menu[i] + " "
                            }
                            InfoTools.ToastTools(context, "选择了::${result}")
                        }.setCancelable(false)
                        .create().show()


                }

            }
        }

    }

}
